require.config({
    baseUrl:"./modules",
    paths:{
        jq:"../libs/jquery",
        "cookie":"../libs/jquery.cookie",
        number:"../js/number"
    },
    shim:{
        cookie:{
            deps:["jq"]
        }
    }
});
require(["cookie","getGoods","cartRender","changeLocal","allCheck","number"],(_cookie,gg,cr,cl,ac,_number)=>{
    let goods = $.cookie("goods") ? JSON.parse($.cookie("goods")) : [];
    if(goods.length < 1){
        $(".c-c .empty").html(`<p>当前购物车为空,<a href="./list.html">去选购</a>
        </p>`)
        return;}
    const url = "http://localhost:3000/api"
    gg(url,"getGoods",res=>{
        //渲染页面
        cr($(".c-c"),res,goods);

        //  $(document).keydown(function(){
        //     if(event.keydown === 13){
        //         cr($(".c-c"),res.filter(val=>val.border.includes($("search").val())||val.kind.includes($("search").val())||val.proName.includes($("search").val()))),goods
        //     }
        // })

    //     $("#priceSort").change(function(){
    //         let arr = [...res];
    //         switch($(this).val()){
    //             case "2":
    //                 arr.sort((a, b)=>a.price - b.price);
    //                 lr($(".box"), arr);
    //                 break;
    //             case "3":
    //                 arr.sort((a, b)=>b.price - a.price);
    //                 lr($(".box"), arr);
    //                 break;
    //             case "1":
    //                 lr($(".box"), res);
    //                 break;
    //         }
    //     })
    // });

        for(let i=0; i<$(".count").length; i++){
            const n = new InputNumber({
                min:1,
                max:100,       
                value: $(".count")[i].getAttribute("data-num"),
                cont: $(".count")[i]
            }); 
        }
    })

    

    //选中状态改变实际
    $(".c-c").on("click",".check",function(){
        const id = $(this).parent().attr("data-id");
        const f = this.checked;
        cl(goods,id,i=>{
            goods[i].flag = f;
        })

        $("#allCheck")[0].checked = goods.every(val =>val.flag);
        $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));
        
        $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
        
    })
    
    $(".c-c").on("input",".ipt",function(){
        const id= $(this).parent().parent().parent().attr("data-id");
        let n = $(this).val()-0;
        // console.log(n);
	   
        // $(".setNum").html(n);
        cl(goods,id,i=>{
            goods[i].num = n;
        })
        $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));
        
        $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
       

        
    })
    // 右按钮
    $(".c-c").on("click",".add",function(){
        const id= $(this).parent().parent().parent().attr("data-id");
        let n = $(this).parent().children(".ipt").val()-0;
        // console.log(n);
	    // n++;
        // $(".ipt").val(n);
        cl(goods,id,i=>{
            goods[i].num = n;
        })
        $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));
        
        $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
        
    })
    $(".c-c").on("click",".reduce",function(){
        const id= $(this).parent().parent().parent().attr("data-id");
        let n = $(this).parent().children(".ipt").val()-0;
        // console.log(n);
	    
        cl(goods,id,i=>{
            goods[i].num = n;
        })
        $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));
        
        $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
        
    })
    //删除
    $(".c-c").on("click",".del",function(){
        const id=$(this).parent().parent().attr("data-id");
        $(this).parent().parent().remove();
        cl(goods,id,i=>{
            goods.splice(i,1);
        })

        $("#allCheck")[0].checked = goods.every(val =>val.flag);
   

        $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));
        
        $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
        
    })

    //全选影响单个
    $("#allCheck").click(function(){
        ac(goods,this.checked,$(".check"));

        $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));

        $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
    })
    //单个影响全选
    $("#allCheck")[0].checked = goods.every(val =>val.flag);
   
    //总数量的计算
    $("#allNum").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num : 0),0));
    //总价的计算
    $("#allPrice").html(goods.reduce((prev,val)=>prev+(val.flag ? val.num*val.price : 0),0));
  
    //删除选中
    $("#delCheck").click(function(){
        [...$(".check")].forEach(val=>{
            if(val.checked){
                $(val).parent().remove();
                $("#allPrice").html("0");
                $("#allNum").html("0");
              
            }
        })
        goods = goods.filter(val=>{
            return !val.flag;
        })
        $.cookie("goods",JSON.stringify(goods));
    })

})